package com.wwcom.darteagle.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.text.Html;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwcom.darteagle.R;
import com.wwcom.darteagle.model.GlobalAppData;
import com.wwcom.darteagle.model.Match;
import com.wwcom.darteagle.model.MatchPlayer;
import com.wwcom.darteagle.model.VisitScore;
import com.wwcom.darteagle.util.Settings;

public class ScoreMatrixPlayerView extends LinearLayout {
	//private data
	private Match match;
	private MatchPlayer matchPlayer;
	private int visitInd;
	private int themeId;
	private float dpiScale;
	private Context context;
	private boolean isLandscape;
	
	//constructors
	public ScoreMatrixPlayerView(Context context) {
		super(context);
		initialiseSelf(context);
	}

	public ScoreMatrixPlayerView(Context context, AttributeSet attrs) {
		super(context,attrs);
		initialiseSelf(context);
	}
	
	private void initialiseSelf(Context context) {
		View.inflate(context, R.layout.score_matrix_player, this);

		this.context= context;
		themeId= Settings.getThemeID(context);
		
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        dpiScale= metrics.density;

		
		GlobalAppData gd= GlobalAppData.getSingletonInstance();
		match= gd.match;
	    
        isLandscape= context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;		
		
		if (!isLandscape) {
			boolean useSets= match.getNumSets()>0;
	    	if (!useSets) {
	    		TextView setsTitle= ((TextView)this.findViewById(R.id.setsTitle)); 
	    		setsTitle.setText("");
	    		ViewGroup.LayoutParams lp = setsTitle.getLayoutParams();
	    		lp.width= 0;
	    		setsTitle.setLayoutParams(lp);
	    	}
	    	if (themeId == R.style.Theme_BBC) {
	        	if (useSets)
	        		((TextView)this.findViewById(R.id.setsTitle)).setText(context.getString(R.string.scorematrix_sets_bbc));    		
	        	((TextView)this.findViewById(R.id.legsTitle)).setText(context.getString(R.string.scorematrix_legs_bbc));    		
	        	((TextView)this.findViewById(R.id.ptsTitle)).setText(context.getString(R.string.scorematrix_pts_bbc));
	    	} else if (themeId == R.style.Theme_SKY || themeId == R.style.Theme_SKYWC2012) {
	        	if (useSets)
	        		((TextView)this.findViewById(R.id.setsTitle)).setText(context.getString(R.string.scorematrix_sets_sky));    		
	        	((TextView)this.findViewById(R.id.legsTitle)).setText(context.getString(R.string.scorematrix_legs_sky));    		
	        	((TextView)this.findViewById(R.id.ptsTitle)).setText(context.getString(R.string.scorematrix_pts_sky));    		    		
	    	}
		}
    	
    	matchPlayer= null;
    	visitInd= -1;
    	
    	drawMatrix();
	}

	public void setLegHistoryPosition(MatchPlayer matchPlayer, int visitInd) {
		this.matchPlayer= matchPlayer;
		this.visitInd= visitInd;
		drawMatrix();
	}
	
	public void updateView() {
		 drawMatrix();
	}	
	        
	private void drawMatrix() {
		
		VisitScore vs= null;
		if (matchPlayer == null) {
			vs= match.getCurrentPlayerScore();
		} else {
			vs= match.getVisitScore(matchPlayer,visitInd);
		}
		TextView playerName = (TextView) this.findViewById(R.id.playerName);
		TextView playerPoints = (TextView) this.findViewById(R.id.playerPoints);
		TextView playerLegs = (TextView) this.findViewById(R.id.playerLegs);
		TextView playerSets = (TextView) this.findViewById(R.id.playerSets);
		TextView playerInfoString = (TextView) this.findViewById(R.id.playerInfoString);
		TextView playerInfoSpacer = (TextView) this.findViewById(R.id.playerInfoSpacer);
		TextView playerInfoSpacerAlt = (TextView) this.findViewById(R.id.playerInfoSpacerAlt);
		TextView player9Dart = (TextView) this.findViewById(R.id.player9Dart);
		TextView playerCurrentPoints = (TextView) this.findViewById(R.id.playerCurrentPoints);
		
		MatchPlayer mp= vs.getMatchPlayer();
		playerName.setText(mp.getPlayer().getName().toUpperCase());
		playerPoints.setText(Integer.toString(vs.getLegScore()));
		playerLegs.setText(Integer.toString(mp.getNumLegs()));
    	if (match.getNumSets() == 0) {
    		playerSets.setText("");
    		ViewGroup.LayoutParams lp = playerSets.getLayoutParams();
    		lp.width= 0;
    		playerSets.setLayoutParams(lp);
    	} else {		
    		playerSets.setText(Integer.toString(mp.getNumSets()));
    	}
		String playerInfo= getPlayerInfoString(vs);
		if (playerInfo != null) {	    			    	
			playerInfoString.setText(Html.fromHtml(playerInfo));
    		if (dpiScale > 1.5) {
    			playerInfoString.setHeight(34 * (isLandscape ? 2 : 1));
    		} else {
    			playerInfoString.setHeight(30 * (isLandscape ? 2 : 1));
    		}
			
		} else {
			playerInfoString.setText("");
			playerInfoString.setHeight(0);
			playerInfoSpacer.setHeight(0);
			playerInfoSpacerAlt.setHeight(0);
		}
		if (themeId == R.style.Theme_SKY) {
			playerCurrentPoints.setHeight(0);
			if (vs.get9Dart()) {
				player9Dart.setText("9");
				player9Dart.setBackgroundResource(R.drawable.yellow_ninedart_sky); 
				if (isLandscape) {
		    		ViewGroup.LayoutParams lp = player9Dart.getLayoutParams();
		    		lp.width= (int)(20 * (isLandscape ? 2 : 1) * dpiScale);
		    		player9Dart.setLayoutParams(lp);			
		    		playerInfoSpacer.setLayoutParams(lp);			    		
				}
			} else {
				player9Dart.setText("");
				player9Dart.setBackgroundColor(0x00000000);
				if (isLandscape) {
		    		ViewGroup.LayoutParams lp = player9Dart.getLayoutParams();
		    		lp.width= 0;
		    		player9Dart.setLayoutParams(lp);			
		    		playerInfoSpacer.setLayoutParams(lp);			    		
				}
			}
		} else if (themeId == R.style.Theme_SKYWC2012) {
			playerCurrentPoints.setHeight(0);			
			if (vs.get9Dart()) {
				player9Dart.setText("9");
				player9Dart.setBackgroundResource(R.drawable.yellow_ninedart_sky);
				if (isLandscape) {
		    		ViewGroup.LayoutParams lp = player9Dart.getLayoutParams();
		    		lp.width= (int)(20 * (isLandscape ? 2 : 1) * dpiScale);
		    		player9Dart.setLayoutParams(lp);			
		    		playerInfoSpacer.setLayoutParams(lp);			    		
				}
			} else {
				player9Dart.setText("");
				player9Dart.setBackgroundColor(0x00000000);    			
				if (isLandscape) {
		    		ViewGroup.LayoutParams lp = player9Dart.getLayoutParams();
		    		lp.width= 0;
		    		player9Dart.setLayoutParams(lp);			
		    		playerInfoSpacer.setLayoutParams(lp);			    		
				}
			}
		}
		
		int currentPoints= vs.getLegScore();
		if (currentPoints > 0) {
			playerCurrentPoints.setText(Integer.toString(currentPoints));
        	if (themeId == R.style.Theme_BBC)
        		playerCurrentPoints.setBackgroundResource(R.drawable.black_background_bbc);
		} else {
			playerCurrentPoints.setText("");
    		playerCurrentPoints.setBackgroundDrawable(null);
		}
	}

	private String getPlayerInfoString(VisitScore vs) {
		//checkouts
		String str= null;
		String[] checkout= vs.getCheckout();
		if (checkout != null && checkout.length > 0) {
			str= checkout[0];
			if (checkout.length >= 2) {
				str= str+", "+checkout[1];
			}
			if (checkout.length >= 3) {
				str= str+", "+checkout[2];
			}
        	if (themeId == R.style.Theme_BBC) {
        		str= "<font color=\"#ffff00\">"+context.getString(R.string.scorematrix_checkout)+" </font>"+str;
        	}
		}
		
		return str;
	}
	        
}
